package com.castle.fortress.admin.cms.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.castle.fortress.admin.cms.dto.CmsCustTagDto;
import com.castle.fortress.admin.cms.entity.CmsCustTagEntity;
import com.castle.fortress.admin.cms.mapper.CmsCustTagMapper;
import com.castle.fortress.admin.cms.service.CmsCustTagService;
import com.castle.fortress.common.entity.RespBody;
import com.castle.fortress.common.respcode.BizErrorCode;
import com.castle.fortress.common.utils.ConvertUtil;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * cms用户自定义标签表 服务实现类
 *
 * @author castle
 * @since 2022-03-02
 */
@Service
public class CmsCustTagServiceImpl extends ServiceImpl<CmsCustTagMapper, CmsCustTagEntity> implements CmsCustTagService {
    /**
    * 获取查询条件
    * @param cmsCustTagDto
    * @return
    */
    public QueryWrapper<CmsCustTagEntity> getWrapper(CmsCustTagDto cmsCustTagDto){
        QueryWrapper<CmsCustTagEntity> wrapper= new QueryWrapper();
        if(cmsCustTagDto != null){
            CmsCustTagEntity cmsCustTagEntity = ConvertUtil.transformObj(cmsCustTagDto,CmsCustTagEntity.class);
            wrapper.eq(StrUtil.isNotEmpty(cmsCustTagEntity.getTitle()),"title",cmsCustTagEntity.getTitle());
        }
        wrapper.orderByDesc("create_time");
        return wrapper;
    }


    @Override
    public IPage<CmsCustTagDto> pageCmsCustTag(Page<CmsCustTagDto> page, CmsCustTagDto cmsCustTagDto) {
        QueryWrapper<CmsCustTagEntity> wrapper = getWrapper(cmsCustTagDto);
        Page<CmsCustTagEntity> pageEntity = new Page(page.getCurrent(), page.getSize());
        Page<CmsCustTagEntity> cmsCustTagPage=baseMapper.selectPage(pageEntity,wrapper);
        Page<CmsCustTagDto> pageDto = new Page(cmsCustTagPage.getCurrent(), cmsCustTagPage.getSize(),cmsCustTagPage.getTotal());
        pageDto.setRecords(ConvertUtil.transformObjList(cmsCustTagPage.getRecords(),CmsCustTagDto.class));
        return pageDto;
    }


    @Override
    public List<CmsCustTagDto> listCmsCustTag(CmsCustTagDto cmsCustTagDto){
        QueryWrapper<CmsCustTagEntity> wrapper = getWrapper(cmsCustTagDto);
        List<CmsCustTagEntity> list = baseMapper.selectList(wrapper);
        return ConvertUtil.transformObjList(list,CmsCustTagDto.class);
    }

    @Override
    public RespBody checkColumnRepeat(CmsCustTagDto dto) {
        //校验code
        QueryWrapper<CmsCustTagEntity> queryWrapper=new QueryWrapper();
        queryWrapper.eq("title",dto.getTitle());
        List<CmsCustTagEntity> list = baseMapper.selectList(queryWrapper);
        if(list != null && !list.isEmpty()){
            //新增
            if(dto.getId() == null){
                return RespBody.fail(BizErrorCode.CMS_TAG_CODE_EXIST_ERROR);
                //修改
            }else{
                for(CmsCustTagEntity entity:list){
                    if(!entity.getId().equals(dto.getId())){
                        return RespBody.fail(BizErrorCode.CMS_TAG_CODE_EXIST_ERROR);
                    }
                }
            }
        }
        return RespBody.data("校验通过");
    }

    @Override
    public List<CmsCustTagDto> listByArticle(Long articleId) {
        List<CmsCustTagEntity> list = baseMapper.listByArticle(articleId);
        return ConvertUtil.transformObjList(list,CmsCustTagDto.class);
    }

    @Override
    public List<CmsCustTagDto> cmsTagListCmsTag(Map<String, Object> map) {
        return ConvertUtil.transformObjList(baseMapper.cmsTagListCmsTag(map), CmsCustTagDto.class);
    }
}

